<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24" :xs="24">
        <el-form
          v-show="showSearch"
          ref="queryForm"
          :model="queryGoods"
          :inline="true"
          label-width="100px"
        >

          <el-form-item label="商品名称" prop="status">
            <el-select
              v-model="queryGoods.type"
              placeholder="商品名称"
              clearable
              size="small"
              style="width: 200px"
            >
              <el-option v-for="(item,index) in goodsList" :key="item.id" :label="item.goodsName" :value="item.id"/>

            </el-select>
          </el-form-item>


          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
            >搜索
            </el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
            >新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              @click="handleDelete"
            >删除
            </el-button>
          </el-col>

          <!-- <right-toolbar
            :show-search.sync="showSearch"
            :columns="columns"
            @queryTable="getList"
          /> -->
        </el-row>

        <el-table :data="goodsList" style="width: 100%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55"/>
          <el-table-column prop="goodsName" label="商品名称" align="center"/>
          <el-table-column  v-if="false" prop="id" label="id" align="center" hidden/>
          <el-table-column prop="goodsPoint" label="所需积分" align="center"/>
          <el-table-column prop="goodsTotal" label="商品数量" align="center"/>
          <el-table-column prop="goodsNumber" label="剩余库存" align="center">
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" align="center"/>

          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template v-if="scope.row.id !== 1" slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <el-pagination
      :page-size.sync="queryGoods.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      :page-sizes="[10, 20, 30, 40]"
      :current-page.sync="queryGoods.pageNum"
      @current-change="getList"
      @size-change="getList"
    />
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form"  :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="商品名称" prop="goodsName">
              <el-input
                v-model="form.goodsName"
                placeholder="请输入商品名称"
                maxlength="30"
              />
            </el-form-item>

            </el-col>
          </el-row>

        <el-row>

          <el-col :span="12">
            <el-form-item label="商品积分值" prop="goodsPoint">
              <el-input
                v-model="form.goodsPoint"
                placeholder="请输入积分值"
                type="number"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商品库存" prop="goodsNumber">
              <el-input
                v-model="form.goodsNumber"
                placeholder="请输入商品库存"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
        </el-row>
            <el-form-item label="商品简介" prop="goodsIntroduce">
              <el-input
                v-model="form.goodsIntroduce"
                placeholder="请输入商品简介"
                maxlength="30"
                type="textarea"
              />
            </el-form-item>



      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {allGoodsList,addGoods, delGoods,updateGoods} from "@/api/point/goods";

export default {
  name: 'Exchange',
  data() {
    let self = this;
    var checkNumber = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入库存值'));
      } else if (value <= 0) {
        callback(new Error('库存值必须大于0!'));
      } else {
        callback();
      }
    };
    var checkPoint = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入商品兑换所需积分'));
      } else if (value <= 0) {
        callback(new Error('积分值必须大于0!'));
      } else {
        callback();
      }
    };
    return {
      // 查询参数
      queryGoods:{
        pageNum: 1,
        pageSize: 10,
        type:undefined
      },
      title: '',
      // 是否显示弹出层
      open: false,
      // 显示搜索条件
      showSearch: true,
      // 遮罩层
      loading: true,
      // 用户表格数据
      exchangeList: null,
      goodsList: null,
      // 总条数
      total: 0,
      // 选中数组
      ids: [],
      // 表单参数
      form: {},
      rules: {
        goodsName: [
          {required: true, message: '商品名称不能为空', trigger: 'blur'},
        ],
        goodsPoint: [
          {required: true, trigger: 'blur',validator:checkPoint}
        ],
        goodsNumber: [
          {required: true, trigger: 'blur',validator:checkNumber}
        ],
        goodsIntroduce: [
          {required: true, message: '商品介绍不能为空', trigger: 'blur'},
        ]
      }
    }
  },
  watch: {},
  created() {
    this.getList()
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryGoods.pageNum = 1
      this.getList()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    getList() {
      this.loading = true
      allGoodsList(this.queryGoods).then((response) => {
        this.goodsList = response.list
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      this.form = row
      this.open = true
      this.title = '修改商品'
    },
    // 表单重置
    reset() {
      this.form = {
        userName: undefined,
        score: undefined,
        scoreDetail: undefined,
        type: undefined,
        userId: undefined,
        id:undefined
      }
      this.resetForm('form')
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否删除当前选中数据项？')
        .then(function () {
          return delGoods(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {
        })
    },
    handleAdd() {
      this.reset()
        this.open = true
        this.title = '添加商品'
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id !== undefined) {
            updateGoods(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          }else{
            addGoods(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    }
  }
}
</script>
